package org.dromara.netbar.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.netbar.service.INetbarMemberService;
import org.dromara.system.domain.vo.SysRoleVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.netbar.domain.vo.NetbarMemberLevelVo;
import org.dromara.netbar.domain.bo.NetbarMemberLevelBo;
import org.dromara.netbar.service.INetbarMemberLevelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 会员等级管理
 *
 * @author Lion Li
 * @date 2025-06-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/netbar/memberLevel")
public class NetbarMemberLevelController extends BaseController {

    private final INetbarMemberLevelService netbarMemberLevelService;

    private final INetbarMemberService netbarMemberService;

    /**
     * 查询会员等级管理列表
     */
    @SaCheckPermission("netbar:memberLevel:list")
    @GetMapping("/list")
    public TableDataInfo<NetbarMemberLevelVo> list(NetbarMemberLevelBo bo, PageQuery pageQuery) {
        return netbarMemberLevelService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取会员等级选择框列表
     *
     * @param memberLevelIds 角色ID串
     */
    @SaCheckPermission("netbar:memberLevel:query")
    @GetMapping("/optionselect")
    public R<List<NetbarMemberLevelVo>> optionselect(@RequestParam(required = false) Long[] memberLevelIds) {
        return R.ok(netbarMemberLevelService.listMemberLevelByIds(memberLevelIds == null ? null : List.of(memberLevelIds)));
    }

    /**
     * 导出会员等级管理列表
     */
    @SaCheckPermission("netbar:memberLevel:export")
    @Log(title = "会员等级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NetbarMemberLevelBo bo, HttpServletResponse response) {
        List<NetbarMemberLevelVo> list = netbarMemberLevelService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员等级管理", NetbarMemberLevelVo.class, response);
    }

    /**
     * 获取会员等级管理详细信息
     *
     * @param levelId 主键
     */
    @SaCheckPermission("netbar:memberLevel:query")
    @GetMapping("/{levelId}")
    public R<NetbarMemberLevelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long levelId) {
        return R.ok(netbarMemberLevelService.queryById(levelId));
    }

    /**
     * 新增会员等级管理
     */
    @SaCheckPermission("netbar:memberLevel:add")
    @Log(title = "会员等级管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NetbarMemberLevelBo bo) {
        return toAjax(netbarMemberLevelService.insertByBo(bo));
    }



    /**
     * 修改会员等级管理
     */
    @SaCheckPermission("netbar:memberLevel:edit")
    @Log(title = "会员等级管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NetbarMemberLevelBo bo) {
        return toAjax(netbarMemberLevelService.updateByBo(bo));
    }

    /**
     * 删除会员等级管理
     *
     * @param levelIds 主键串
     */
    @SaCheckPermission("netbar:memberLevel:remove")
    @Log(title = "会员等级管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{levelIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] levelIds) {
        Map<Long,Integer> memberLevelCountMap = netbarMemberService.countMapByMemberLevelIds(levelIds);
        if (null != memberLevelCountMap && !memberLevelCountMap.keySet().isEmpty()) {
            return R.fail("会员等级下有会员，请先删除会员");
        }
        return toAjax(netbarMemberLevelService.deleteWithValidByIds(List.of(levelIds), true));
    }
}
